package cn.xinyangtong.manager.service.impl;

import cn.xinyangtong.common.pojo.DataResult;
import cn.xinyangtong.common.pojo.DefaultResult;
import cn.xinyangtong.manager.dao.JedisClient;
import cn.xinyangtong.manager.service.ItemService;
import cn.xinyangtong.mapper.TbItemDescMapper;
import cn.xinyangtong.mapper.TbItemMapper;
import cn.xinyangtong.mapper.TbItemParamItemMapper;
import cn.xinyangtong.pojo.TbItem;
import cn.xinyangtong.pojo.TbItemDesc;
import cn.xinyangtong.pojo.TbItemExample;
import cn.xinyangtong.pojo.TbItemParamItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商品
 * Created by fengbo on 2017/6/8.
 */
@Service("itemService")
public class ItemServiceImpl implements ItemService {

    private final Logger log = LoggerFactory.getLogger(ItemServiceImpl.class);

    @Autowired
    private TbItemMapper itemMapper;
    @Autowired
    private TbItemDescMapper itemDescMapper;
    @Autowired
    private TbItemParamItemMapper itemParamItemMapper;
    @Autowired
    private JedisClient jedisClient;

    //商品列表查询
    @Override
    public DataResult<TbItem> getItemList(Integer page, Integer rows) {
        TbItemExample example = new TbItemExample();
        //分页处理
        PageHelper.startPage(page, rows);
        final List<TbItem> list = itemMapper.selectByExample(example);
        //取出总记录数
        final PageInfo<TbItem> pageInfo = new PageInfo<>(list);

        //创建一个返回值对象
        final DataResult<TbItem> result = new DataResult<>();
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    //添加商品
    @Transactional
    @Override
    public DefaultResult addItem(TbItem item, String desc, String itemParam) {
        //生成商品id
        //使用redis的自增长key
        final Long itemId = jedisClient.incr("itemId");
        //补全不完整的字段
        item.setId(itemId);
        item.setStatus((byte) 1);
        final Date date = new Date();
        item.setCreated(date);
        item.setUpdated(date);
        try {
            //把数据插入到商品表
            int num = itemMapper.insert(item);
            if (num <= 0) {
                log.error("Insert Item Fail! ItemId = " + itemId);
                throw new RuntimeException();
            }
            //添加商品描述
            addItemDesc(itemId, desc);
            addItemParamItem(itemId, itemParam);
        } catch (Exception e) {
            log.error("Add Item Fail! ItemID = " + itemId + ";Message:" + e.getMessage());
            throw new RuntimeException();
        }
        return DefaultResult.ok();
    }

    //上架、下架、删除商品
    @Override
    public DefaultResult updateStatus(byte status, Long[] ids) {
        final TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        final TbItem item = new TbItem();
        item.setStatus(status);
        int num = itemMapper.updateByExampleSelective(item, example);
        if (num > 0) {
            return DefaultResult.ok();
        } else {
            log.warn("Update Status Failed,ItemId = " + Arrays.toString(ids));
            return DefaultResult.build(500, "操作失败！");
        }
    }

    //根据商品id查询商品描述信息
    @Override
    public DefaultResult getItemDescByItemId(Long itemId) {
        TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
        return DefaultResult.ok(itemDesc);
    }

    /**
     * 添加商品描述信息
     */
    private void addItemDesc(Long itemId, String desc) {
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemDesc(desc);
        itemDesc.setItemId(itemId);
        Date date = new Date();
        itemDesc.setCreated(date);
        itemDesc.setUpdated(date);
        int num = itemDescMapper.insert(itemDesc);
        if (num <= 0) {
            log.error("Insert ItemDesc Fail! ItemId = " + itemId + "Desc = " + desc);
            throw new RuntimeException();
        }
    }

    /***
     * 添加商品规格参数
     */
    private void addItemParamItem(Long itemId, String itemParam) {
        TbItemParamItem itemParamItem = new TbItemParamItem();
        itemParamItem.setItemId(itemId);
        itemParamItem.setParamData(itemParam);
        Date date = new Date();
        itemParamItem.setCreated(date);
        itemParamItem.setUpdated(date);
        int num = itemParamItemMapper.insert(itemParamItem);
        if (num <= 0) {
            log.error("Insert ItemParamItem Fail! ItemId = " + itemId + "ItemParam = " + itemParam);
            throw new RuntimeException();
        }
    }

}
